//
//  registerViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/25.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "registerViewController.h"
#import "DataBaseManager.h"
#import "ContactModel.h"
@interface registerViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *RepassText;
@property (weak, nonatomic) IBOutlet UITextField *mailboxText;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (nonatomic, strong) NSMutableArray *nameArr;

@end

@implementation registerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"registerViewController" owner:self options:nil] lastObject];
    self.navigationItem.title = @"注册";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerOK)];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"用户登陆" style:UIBarButtonItemStylePlain target:self action:@selector(backLogin)];
    self.passwordText.secureTextEntry = YES;
    self.RepassText.secureTextEntry = YES;
}

/// 返回登录页面
//- (void)backLogin
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

/// 注册按钮

-(void)registerOK
{
    // 如果输入正确就注册成功
    // 判断输入格式是不是有问题
    if (self.userText.text.length != 0 && self.passwordText.text.length != 0 && self.RepassText.text.length != 0 && self.passwordText.text == self.RepassText.text) {
        NSLog(@"hhhh");
        
        // 判断当前的name是否已经存在了,如果存在就提示该用户名已经存在
        [[DataBaseManager shareInstance] openDB];
        NSMutableArray *arr = [[DataBaseManager shareInstance] select];
        for (ContactModel *m in arr) {
            self.nameArr = [NSMutableArray array];
            [self.nameArr addObject:m.contactName];
        }
        for (NSString *temp in _nameArr) {
            if ([self.userText.text isEqualToString:temp]) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"当前用户已经存在!" preferredStyle:UIAlertControllerStyleAlert];
                __weak UIAlertController *art = alert;
                [self presentViewController:alert animated:YES completion:^{
                    [art dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                }];
                
            }
        }
        
        
        // 将注册的联系人的姓名个密码保存到数据库里面
        //创建单例对象并打开数据库
        DataBaseManager *dbManager = [DataBaseManager shareInstance];
        [dbManager openDB];
        // 创建表格
        [[DataBaseManager shareInstance] createTable];
        
        //创建存储联系人信息的model
        ContactModel *model = [ContactModel new];
        model.contactName = _userText.text;
        model.password = _passwordText.text;
        model.mailbox = _mailboxText.text;
        model.phone = _phoneText.text;
        //将联系人的信息存到数据库里面
        [[DataBaseManager shareInstance] insertContact:model];
        
        // 保存BOOL值确认已经是登陆的状态
        // 创建单例 :
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setBool:YES forKey:@"didLogin"];
        [user synchronize];
        
        
        UIAlertController *art = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
        // 改变按钮的状态
        [user setBool:NO forKey:@"button"];
        BOOL b = [[NSUserDefaults standardUserDefaults] boolForKey:@"button"];
        b = !b;
        // 注册成功后将当前的用户名存到单例里面
        [user setObject:_userText.text forKey:@"userName"];
        
        // 提示注册成功后 返回注册界面后 返回登陆界面 再返回最初的界面
        __weak registerViewController *registers = self;
       __weak UIAlertController *sv = art;
        [self presentViewController:art animated:YES completion:^{
//            NSLog(@"靠靠靠");
            [sv dismissViewControllerAnimated:YES completion:^{
                [registers.navigationController popViewControllerAnimated:YES];
                [self.backDelegate backPage];
            }];
        }];
        //将我的页面的按钮状态改变
        
    }
    // 否则提示不正确
    else
    {
        NSLog(@"不对");
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示!" message:@"您的输入格式不正确,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
        __weak UIAlertController *sv = alert;
        [self presentViewController:alert animated:YES completion:^{
            [sv dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
    }
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
